package com.hss.transition;

import android.app.Activity;
import android.content.Intent;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.ActivityOptionsCompat;
import android.support.v4.util.Pair;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.LinearSnapHelper;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.SnapHelper;
import android.util.Log;
import android.view.View;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.hss.recyclerviewtest.R;
import com.hss.recyclerviewtest.decoration.GridSpacingItemDecoration;
import com.hss.recyclerviewtest.itemtouch.OnItemClickListener;
import com.hss.recyclerviewtest.itemtouch.RecyclerViewItemTouchListener;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.footer.ClassicsFooter;
import com.scwang.smartrefresh.layout.header.ClassicsHeader;
import com.scwang.smartrefresh.layout.listener.OnLoadMoreListener;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;

import java.util.ArrayList;
import java.util.List;

public class ShareListActivity extends AppCompatActivity {
    RecyclerView recyclerView;
    BaseQuickAdapter adapter;
    final String TAG = ShareListActivity.class.getSimpleName();
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.smart_refresh_recyclerview);

        RefreshLayout refreshLayout = (RefreshLayout)findViewById(R.id.refreshLayout);

        recyclerView = findViewById(R.id.recyclerView);
        initRecyclerView();
        refreshLayout.setPrimaryColorsId(R.color.colorPrimary, android.R.color.white);
        refreshLayout.setDragRate(0.5f);//显示下拉高度/手指真实下拉高度=阻尼效果
        refreshLayout.setReboundDuration(300);//回弹动画时长（毫秒）
        refreshLayout.setEnableRefresh(true);//是否启用下拉刷新功能
        refreshLayout.setEnableLoadMore(true);//是否启用上拉加载功能
        refreshLayout.setEnablePureScrollMode(false);//是否启用纯滚动模式
        refreshLayout.setEnableNestedScroll(false);//是否启用嵌套滚动
        refreshLayout.setEnableOverScrollBounce(true);//是否启用越界回弹
        refreshLayout.setEnableScrollContentWhenLoaded(true);//是否在加载完成时滚动列表显示新的内容
        refreshLayout.setEnableHeaderTranslationContent(true);//是否下拉Header的时候向下平移列表或者内容
        refreshLayout.setEnableFooterTranslationContent(true);//是否上拉Footer的时候向上平移列表或者内容
        refreshLayout.setEnableLoadMoreWhenContentNotFull(false);//是否在列表不满一页时候开启上拉加载功能
        refreshLayout.setEnableFooterFollowWhenLoadFinished(false);//是否在全部加载结束之后Footer跟随内容1.0.4
        refreshLayout.setEnableOverScrollDrag(false);//是否启用越界拖动（仿苹果效果）1.0.4

        refreshLayout.setEnableScrollContentWhenRefreshed(true);//是否在刷新完成时滚动列表显示新的内容 1.0.5
        refreshLayout.setRefreshHeader(new ClassicsHeader(this));//设置Header
        refreshLayout.setRefreshFooter(new ClassicsFooter(this));//设置Footer
        refreshLayout.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(@NonNull final RefreshLayout refreshLayout) {
                refreshLayout.getLayout().postDelayed(new Runnable() {
                    @Override
                    public void run() {
//                        mAdapter.refresh(initData());
//                        List<String > list = new ArrayList<>();
//                        for (int i=0;i<20;i++){
//                            list.add(refresh_count +"-"+String .valueOf(i));
//                        }
//                        messagesAdapter.setNewData(list);
//                        refreshLayout.finishRefresh();
//                        refreshLayout.setNoMoreData(false);
//                        refresh_count++;
                    }
                }, 2000);
            }
        });

        refreshLayout.setOnLoadMoreListener(new OnLoadMoreListener() {
            @Override
            public void onLoadMore(@NonNull final RefreshLayout refreshLayout) {
                refreshLayout.getLayout().postDelayed(new Runnable() {
                    @Override
                    public void run() {
//                        mAdapter.refresh(initData());
//                        refreshLayout.finishRefresh();
//                        List<String > list = new ArrayList<>();
//                        int start = messagesAdapter.getItemCount();
//                        for (int i=0;i<10;i++){
//                            list.add(refresh_count +"-"+String .valueOf(start+i));
//                        }
//                        messagesAdapter.addData(list);
                        refreshLayout.finishLoadMore();
//                        refreshLayout.finishLoadMoreWithNoMoreData();//将不会再次触发加载更多事件
//                        mAdapter.loadMore(initData());
//                        refreshLayout.finishLoadMore();
                    }
                }, 2000);
            }
        });
    }

    void initRecyclerView() {
        recyclerView.setFocusable(false);
        recyclerView.setLayoutManager(new GridLayoutManager(this, 2));
        recyclerView.setNestedScrollingEnabled(false);
        recyclerView.addItemDecoration(new GridSpacingItemDecoration(2, 5, false));
        adapter = new MyAdapter();

        for (int i=1;i<21;i++){
            adapter.addData(i);
        }
        recyclerView.setAdapter(adapter);
        adapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                ActivityOptionsCompat activityOptionsCompat =null;


                Pair<View, String> pair1 = new Pair<View, String>(view.findViewById(R.id.imageview),getString(R.string.share_element_imageview));
                Pair<View, String> pair2 = new Pair<View, String>(view.findViewById(R.id.header), getString(R.string.share_element_header));
                Pair<View, String> pair3 = new Pair<View, String>(view.findViewById(R.id.tv_info), getString(R.string.share_element_tv_info));

//                activityOptionsCompat = ActivityOptionsCompat.makeSceneTransitionAnimation(ShareListActivity.this, pair1, pair2, pair3);

//                final Pair<View, String>[] pairs = TransitionHelper.createSafeTransitionParticipants(
//                        ((Activity) mContext), true,
//                        new Pair<>(holder.mImageView, mContext.getString(R.string.share_element_imageview)),
//                        new Pair<>(holder.mHeader, mContext.getString(R.string.share_element_header)),
//                        new Pair<>(holder.mTextView, mContext.getString(R.string.share_element_tv_info)));
//                activityOptionsCompat = ActivityOptionsCompat.makeSceneTransitionAnimation(((Activity) mContext), pairs);


//                activityOptionsCompat = ActivityOptionsCompat.makeCustomAnimation(ShareListActivity.this, android.R.anim.slide_in_left, android.R.anim.slide_out_right);

//                activityOptionsCompat = ActivityOptionsCompat.makeScaleUpAnimation(view, view.getWidth() / 2, view.getHeight() / 2, 1000, 1000);

//                activityOptionsCompat = ActivityOptionsCompat.makeThumbnailScaleUpAnimation(view, BitmapFactory.decodeResource(getResources(), R.drawable.a), 0, 0);

//                activityOptionsCompat = ActivityOptionsCompat.makeClipRevealAnimation(view, view.getWidth() / 2, view.getHeight() / 2, 1000, 1000);


                activityOptionsCompat = ActivityOptionsCompat.makeSceneTransitionAnimation(ShareListActivity.this);


                Intent intent = new Intent(ShareListActivity.this, ShareDetailActivity.class);
                startActivity(intent, activityOptionsCompat.toBundle());



            }
        });


//        recyclerView.addOnItemTouchListener(new RecyclerViewItemTouchListener(recyclerView, new OnItemClickListener() {
//            @Override
//            public void onItemClick(View view) {
//                Log.e(TAG, "onItemClick");
//            }
//
//            @Override
//            public void onItemLongClick(View view) {
//                Log.e(TAG, "onItemLongClick");
//
//            }
//        }));
    }



    class  MyAdapter extends BaseQuickAdapter<Integer,BaseViewHolder> {
        int[] images= new  int[]{R.drawable.a,R.drawable.b,R.drawable.c,R.drawable.d,R.drawable.e,R.drawable.f,R.drawable.g,R.drawable.h,R.drawable.i,R.drawable.j};

        public MyAdapter() {
            super( R.layout.item_recycler_share_elements, null);
        }

        @Override
        protected void convert(BaseViewHolder helper, Integer item) {

        }
    }
}
